from api import client
from common.route import *
# from common.read_yaml import ReadYamlConfigs
import os
import configparser
from api.admin.member_controller_api import MemberControllerApi,login1



class Login(client.HttpClient):

    def __init__(self,admin_info,token):
        # self.login = MemberControllerApi(admin_info).login()
        super().__init__()
        self.username = admin_info["user_name"]
        # print("rrrrr:",self.username)
        self.password = admin_info["password"]
        result = login1(username=self.username,password=self.password)
        # print("3333333:",result)
        assert result['code'] == 200,"前台登录失败"
        self.token = result['data']['token']

        #把token值写入配置文件中
        self.conf_file = os.path.abspath(os.path.dirname(os.path.dirname(__file__)) + r"\configs\token.ini")#报存文件的路径
        config = configparser.ConfigParser()
        print("lllllllppppppppppppppppppp")
        #打开配置文件
        config.read(self.conf_file,encoding='utf-8')
        #在配置文件中设置token的值
        config.set("token","{}".format(token),str(self.token))
        config.write(open(self.conf_file,'w')) # w ：修改配置文件的内容并保存

    # #封装登录
    # # print("88899",login)
    # data0 = login['case_data'][0]['body']
    # # print("1121",data0)
    # def login(self,
    #           username=data0['username']
    #           ,password=data0['password']):
    #     etc = {
    #         "username":username
    #         ,"password":password
    #     }
    #
    #     url = login['url']
    #     method = login['method']
    #     url = self.get_full_url(url,etc=etc)
    #     return self.send(url,method = method)

def login_zs(admin_info,zs_token):
    return Login(admin_info,zs_token)

def login_ls(admin_info,ls_token):
    return Login(admin_info,ls_token)